package com.study.demo.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Slf4j
public class OssDemo {

    //阿里云pdf文件存储目录
//    private static final String PDF_DIR = "paper_pdf/";
//
//    private static final String BUCKET_NAME = "wegrowx-platform";
//
//    private static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
//
//    private static final String OSS_ACCESS_ID = "LTAI5tHMu7By2JPDBMjrRT2g";
//
//    private static final String OSS_ACCESS_SECRET = "5u9eazYeKc1lBFVxbUAFygBW4jWoj5";

    private static final String PDF_DIR = "paper_pdf/";

    private static final String BUCKET_NAME = "sk-app-data";

    private static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";

    private static final String OSS_ACCESS_ID = "LTAI5tJbmQ6csUx3NDEYp4fo";

    private static final String OSS_ACCESS_SECRET = "fwXmp7Jj56Lfn1yAWSar9ORGROCgMS";

    private static final String LOCAL_PATH = "D:\\testoss\\newText.csv";



    /**
     * 上传文件
     */
    public static String uploadFile() {

        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, OSS_ACCESS_ID, OSS_ACCESS_SECRET);
        InputStream inputStream = null;

//        File file = new File("D:\\pdfTest\\HtmlToPDF2.pdf");
        try {
            //获取文件流
//            inputStream = file.getInputStream();

            inputStream = new FileInputStream("D:\\pdfTest\\HtmlToPDF2.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取文件名称
//        String filename = file.getOriginalFilename();
//
//        //1.在文件名称中添加随机唯一的值
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//        filename = uuid + filename;

        //2.把文件按日期分类
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter localDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String datePath = localDate.format(localDateFormatter);

        String filename = PDF_DIR + datePath + "/" + uuid + ".pdf";

        //调用OSS方法实现上传
        ossClient.putObject(BUCKET_NAME, filename, inputStream);
        ossClient.shutdown();
        //把上传之后的文件路径返回
        //需要把上传到阿里云oss路径手动拼接出来
        //路径规则：https://edu-cgq.oss-cn-chengdu.aliyuncs.com/QQ%E5%9B%BE%E7%89%8720200502111905.jpg
        String url = "https://" + BUCKET_NAME + "." + ENDPOINT + "/" + filename;
        return url;
    }

    /**
     * 下载
     */
    public static void download( String fileName ) throws IOException {
//        (ENDPOINT, OSS_ACCESS_ID, OSS_ACCESS_SECRET

//        String fileName = "";
        // 创建 OSSClient 实例
        OSSClient ossClient = new OSSClient(ENDPOINT, OSS_ACCESS_ID, OSS_ACCESS_SECRET);
// 判断 bucket 是否存在
        Boolean flag_bucket = ossClient.doesBucketExist(BUCKET_NAME);
// 判断 要下载的文件 是否存在
        Boolean flag_file = ossClient.doesObjectExist(BUCKET_NAME, fileName);

//        OSSObject ossObject = ossClient.getObject(BUCKET_NAME, fileName);

        ossClient.getObject(new GetObjectRequest(BUCKET_NAME, fileName), new File(LOCAL_PATH));

//        ossObject.close();

        ossClient.shutdown();
    }


    public static void main(String[] args) throws IOException {
        String filename = "car_model/20220425/000000000001000.csv";
        download(filename);
    }


}
